package sketch.specs;

import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

import sketch.ast.ASTUtils;
import sketch.generator.ValueGenerator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class DeclaredValueExtractorTest extends TestCase {
	
	public static Test suite() {
		return new TestSuite(DeclaredValueExtractorTest.class);
	}
	
	public void testValueExtractor() {
//		String filePath = "./tests/sketch/specs/samplecode.txt";
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getMethodsWithAnnotation(unit, "TestSketch");
		//get the comment list
		List<Comment> cs = unit.getCommentList();
		//unit.getLineNumber(position)
		//cs = ASTUtils.blockComments(cs);
		for(MethodDeclaration method : methods) {
			System.out.println("  ---- processing method to see the value declaration: " + method.getName() + " ---- ");
			BlockMappingVisitor visitor = new BlockMappingVisitor(cs);
			visitor.visit(method);
			Map<Block, Comment> blockCommentMappings = visitor.getBlockMappings();
			for(Block block : blockCommentMappings.keySet()) {
				SpecValueDeclarationFinder finder = new SpecValueDeclarationFinder(block);
				List<VariableDeclarationStatement> varDecls = finder.getAllValueDeclaredStatements();
				for(VariableDeclarationStatement varDecl : varDecls) {
					System.out.println("  " + varDecl);
					//test the declared values extractor
					List<VariableDeclarationFragment> fragments = varDecl.fragments();
					for(VariableDeclarationFragment fragment : fragments) {
					    DeclaredValueExtractor extractor = new DeclaredValueExtractor(fragment);
					    List<Expression> args = extractor.getArgumentExpression();
					    for(Expression arg : args) {
						    System.out.println("      " + arg);
					    }
					    ValueGenerator generator = extractor.getArgumentValueGenerator();
					    while(generator.hasNext()) {
						    Object obj = generator.next();
						    System.out.println("   the object is: " + obj);
					    }
					}
				}
				System.out.println(block);
			}
		}
	}

}
